package com.chengweiyi.security.security;

import com.chengweiyi.servicebase.utils.R;
import com.chengweiyi.servicebase.utils.ResponseUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;

/**
 * @Author xiao hu die
 * @Date 2022/5/11 19:26
 * @Version 1.0
 */
@Component
public class TokenLogoutHandler implements LogoutHandler {
    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;
    public TokenLogoutHandler (TokenManager tokenManager,RedisTemplate redisTemplate){
        this.tokenManager =tokenManager;
        this.redisTemplate = redisTemplate;
    }
    @Override
    public void logout(HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Authentication authentication) {
        String token = httpServletRequest.getHeader("token");
        if (token!=null){
        /**从Redis中删除这个key*/
        redisTemplate.delete(token);
        }
        ResponseUtil.out(httpServletResponse, R.ok());
    }
}
